﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using System.Collections;
using Telerik.Web.UI;
using System.Drawing;
using System.Globalization;

namespace BVVD_Website.ql_danh_muc
{
    public partial class frmLophoc2 : System.Web.UI.Page
    {
        public static string mfrm = "MNU011";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BVVDDataContext db = new BVVDDataContext();
                if (Session["user"] != null)
                {
                    LST_NSD user = Session["user"] as LST_NSD;

                    V_PHANQUYEN pq = (from p in db.V_PHANQUYENs
                                      where p.MAMN.Equals(mfrm) && p.MANSD.Equals(user.MANSD)
                                      select p).SingleOrDefault();
                    if (pq != null)
                    {
                        grvLophoc.MasterTableView.CommandItemSettings.ShowAddNewRecordButton = pq.ADD > 0 ? true : false;
                        grvLophoc.MasterTableView.Columns.FindByUniqueName("btnDelete").Display = pq.DELETE > 0 ? true : false;
                        grvLophoc.MasterTableView.Columns.FindByUniqueName("btnEdit").Display = pq.EDIT > 0 ? true : false;
                    }
                }

                grvLophoc.Culture = new CultureInfo("vi-VN");
                grvLophoc.Rebind();

                fillgrvLophoc();
                grvLophoc.DataBind();

            }
        }
        protected void grvLophoc_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            //grvLophoc.DataSource = getData();
            fillgrvLophoc();
        }

        private List<LST_LOPHOC> getData()
        {
            BVVDDataStore<LST_LOPHOC> db = new BVVDDataStore<LST_LOPHOC>();
            return db.getFromDB(new Hashtable());

        }
        protected void grvLophoc_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridDataItem item = (GridDataItem)e.Item;
            string recid = item.OwnerTableView.DataKeyValues[item.ItemIndex]["RECID"].ToString();
            BVVDDataContext db = new BVVDDataContext();
            LST_LOPHOC lophoc = (from dm in db.LST_LOPHOCs
                                 where dm.RECID.ToString().Equals(recid)
                                 select dm).SingleOrDefault();
            string delLH = "Xóa thành công lớp học có mã <font color='red'>" + lophoc.MALH +" </font>";
            db.LST_LOPHOCs.DeleteOnSubmit(lophoc);

            try
            {
                db.SubmitChanges();
                rnAlert.Text = delLH;

            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra. \n <br> Nguyên nhân : " + ex.Message;
            }

            rnAlert.Show();
        }
        protected void grvLophoc_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;
            LST_LOPHOC lophoc = new LST_LOPHOC
            {
                MALH = (insertedItem.FindControl("txtMALH") as RadTextBox).Text,
                TENLH = (insertedItem.FindControl("txtTENLH") as RadTextBox).Text,
                KIEULOPHOC = Convert.ToInt32((insertedItem.FindControl("ddlKIEULOPHOC") as RadDropDownList).SelectedValue),
                TONG_TGH = (insertedItem.FindControl("ddlTONG_TGH") as RadDropDownList).SelectedValue,
                HOCPHI = Convert.ToInt32((insertedItem.FindControl("txtHOCPHI") as RadNumericTextBox).Value),
                GHICHU = (insertedItem.FindControl("txtGHICHU") as RadTextBox).Text,
                TRANGTHAI = (insertedItem.FindControl("rblTRANGTHAI") as RadioButtonList).SelectedValue
            };
            BVVDDataContext db = new BVVDDataContext();
            db.LST_LOPHOCs.InsertOnSubmit(lophoc);
            try
            {
                db.SubmitChanges();
                rnAlert.Text = "Thêm mới lớp học <font color='red'>" + lophoc.TENLH + " </font> thành công";
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra \n <br> Nguyên nhân : " + ex.Message;

            }


            rnAlert.Show();
        }
        protected void grvLophoc_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {

            BVVDDataContext db = new BVVDDataContext();
            GridEditableItem editedItem = e.Item as GridEditableItem;
            string recid = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["RECID"].ToString();

            LST_LOPHOC lophoc = (from item in db.LST_LOPHOCs
                                 where item.RECID.ToString().Equals(recid)
                                 select item).SingleOrDefault();

            lophoc.TENLH = (editedItem.FindControl("txtTENLH") as RadTextBox).Text;
            lophoc.TONG_TGH = (editedItem.FindControl("ddlTONG_TGH") as RadDropDownList).SelectedValue;
            lophoc.KIEULOPHOC = Convert.ToInt32((editedItem.FindControl("ddlKIEULOPHOC") as RadDropDownList).SelectedValue);
            lophoc.HOCPHI = Convert.ToInt32((editedItem.FindControl("txtHOCPHI") as RadNumericTextBox).Value);
            lophoc.GHICHU = (editedItem.FindControl("txtGHICHU") as RadTextBox).Text;
            lophoc.TRANGTHAI = (editedItem.FindControl("rblTRANGTHAI") as RadioButtonList).SelectedValue;
            try
            {
                db.SubmitChanges();
                rnAlert.Text = "Cập nhật thành công lớp học có mã <font color='red'> " + lophoc.MALH +"</font>";
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra. \n <br> Nguyên nhân : " + ex.Message;
            }

            rnAlert.Show();
        }

        protected void grvLophoc_ItemDataBound(object sender, GridItemEventArgs e)
        {
            
            
            if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                GridEditFormItem editFormItem = e.Item as GridEditFormItem;
                GridDataItem parentItem = editFormItem.ParentItem;
                RadioButtonList rblTRANGTHAI = editFormItem.FindControl("rblTRANGTHAI") as RadioButtonList;
                RadTextBox txtMALH = editFormItem.FindControl("txtMALH") as RadTextBox;
                RadDropDownList ddlKIEULOPHOC = editFormItem.FindControl("ddlKIEULOPHOC") as RadDropDownList;
                //string txtHELOP = editFormItem["KIEULOPHOC"].Text;
                LinkButton btnUpdate = editFormItem.FindControl("btnUpdate") as LinkButton;
                if (e.Item is GridEditFormInsertItem || e.Item is GridDataInsertItem)
                {


                    txtMALH.ReadOnly = false;                }
                else
                {
                    Label lblTRANGTHAI = editFormItem["TRANGTHAI"].FindControl("lblTRANGTHAI") as Label;
                    //Response.Write("<script>alert('" + txtHELOP + "')</script>");
                    rblTRANGTHAI.SelectedValue = lblTRANGTHAI.Text.Equals("ACTIVE") ? "1" : "0";
                    ddlKIEULOPHOC.SelectedValue = editFormItem["KIEULOPHOC"].Text.Equals("Liên tục") ? "1" : (editFormItem["KIEULOPHOC"].Text.Equals("Bổ túc") ? "2" : "3");
                    
                   // txtMALH.Text = lbel.Text;
                    txtMALH.ReadOnly = true;
                    if (!grvLophoc.MasterTableView.Columns.FindByUniqueName("btnEdit").Display)
                    {
                        btnUpdate.Enabled = false;
                    }

                }


            }
        }

        private void fillgrvLophoc() {
            BVVDDataContext db = new BVVDDataContext();
            var list = (from lh in db.LST_LOPHOCs
                        select new
                        {
                            RECID = lh.RECID,
                            MALH = lh.MALH,
                            TENLH = lh.TENLH,
                            KIEULOPHOC = lh.KIEULOPHOC==1?"Liên tục":(lh.KIEULOPHOC==2?"Bổ túc":"Chính quy"),
                            TONG_TGH = lh.TONG_TGH,
                            HOCPHI = lh.HOCPHI,
                            TRANGTHAI = lh.TRANGTHAI,
                            GHICHU = lh.GHICHU
                        }).ToList();
            grvLophoc.DataSource = list;
        
        }
    }
}